# -*- coding:utf-8 -*-

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time
import re


requests.packages.urllib3.disable_warnings()  # 忽略HTTPS安全警告


"""
豆瓣-电影top250
https://movie.douban.com/top250
"""

class DouBan_Get():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "User-Agent": ua.random

    }

    # 列表内容
    def get_comments(self, current):
        url = f"https://movie.douban.com/top250?start={current}&filter="
        html = HttpUtils.do_request("GET", url, self.headers, "")
        root = etree.HTML(html.text)
        for r in root.xpath('//ol[@class="grid_view"]/li'):
            dict_data = dict()
            url = r.xpath('.//div[@class="hd"]/a/@href')[0]
            title = r.xpath('.//div[@class="hd"]/a/span[1]/text()')[0]
            title_eng = r.xpath('.//div[@class="hd"]/a/span[2]/text()')[0]
            dict_data['title'] = title + title_eng  # 标题
            dict_data['dy'] = r.xpath('.//div[@class="bd"]/p/text()')[0].replace("\n", "").strip()  # 导演
            tmp = r.xpath('.//div[@class="bd"]/p/text()')[1].replace("\n", "").strip()
            dict_data['year'] = tmp.split("/")[0]  # 年份
            dict_data['country'] = tmp.split("/")[1]  # 国家
            dict_data['type'] = tmp.split("/")[2]  # 类型
            dict_data['star'] = r.xpath('.//div[@class="star"]/span[2]/text()')[0]  # 评分

            print(str(dict_data))
            self.detail(url)

    # 明细
    def detail(self, url):
        html_detail = HttpUtils.do_request("GET", url, self.headers, "")
        root = etree.HTML(html_detail.text)
        dict_data = dict()
        dict_data['title'] = root.xpath('//h1/span[1]/text()')[0]  # 标题
        dict_data['year'] = root.xpath('//h1/span[2]/text()')[0]  # 年份
        dict_data['dy'] = "".join(root.xpath('//div[@id="info"]/span[1]//text()'))  # 导演
        dict_data['bj'] = "".join(root.xpath('//div[@id="info"]/span[2]//text()'))  # 编剧
        dict_data['zy'] = "".join(root.xpath('//div[@id="info"]/span[3]//text()'))  # 主演
        dict_data['lx'] = "/".join(root.xpath('//span[@property="v:genre"]//text()'))  # 类型
        dict_data['desc'] = "".join(root.xpath('//span[@class="all hidden"]//text()'))  # 简介
        print(str(dict_data))


if __name__ == '__main__':
    douban = DouBan_Get()
    for i in range(0, 251, 25):
        print(f"=========={i}=============")
        douban.get_comments(i)